package Games;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import ui.GameFrame;
import ui.Main;

import Client.Command;
import java.awt.event.MouseMotionListener;
import javax.swing.JOptionPane;

public class GamesController implements MouseListener,MouseMotionListener {
	
    protected static GamesModel gamesModel;
    protected static BoardCanvas boardCanvas;
    protected static GameFrame gameFrame;
    
    public static final int PLAYER1 = 0;
    public static final int PLAYER2 = 1;
    public static AbstractFunctions game;

    public GamesController(AbstractFunctions game, GamesModel gamesModel, GameFrame gameFrame) {
    	this.game = game;
        this.gamesModel = gamesModel;
        this.gameFrame = gameFrame;
    }

    public void mouseClicked(MouseEvent e) {
        if(gamesModel.getCurrentPlayer() != null && gamesModel.getCurrentPlayer().equals(gamesModel.getUs()) && Main.mouseInput)
            doMove(e);
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {      
    }

    public void mouseDragged(MouseEvent e) {   
    }
    
    public void setBoardCanvas(BoardCanvas boardCanvas) {
    	this.boardCanvas = boardCanvas;
    }
    
    public static void appendHistory(String string) {
    	gameFrame.getHistoryView().setTxtArea("- "+string+"\n");
    }
    
    public static void setInitialPlayer(String player) {
    	setCurrentPlayer(player);
    	game.initiateBoard(player==gamesModel.getPlayerOne());
    	update();
    }
    
    public static void setCurrentPlayer(String player) {
        gameFrame.getPlayerView().setCurrentPlayer(player);
    }
    
    public static void setPlayerOne(String player) {
        gameFrame.getPlayerView().setPlayerOne(player);
    }
    
    public static void setPlayerTwo(String player) {
        gameFrame.getPlayerView().setPlayerTwo(player);
    }
    
    public static void updateScore() {
        Board board = gamesModel.getBoard();
        int currentPlayer1Score = 0;
        int currentPlayer2Score = 0;
        
        //for field in board
        for(int col = 0; col < board.getWidth(); col++) {
            for(int row = 0; row < board.getHeight(); row++) {
                int val = gamesModel.getBoard().getFieldValue(col, row);
                if (val == PLAYER1){
                    currentPlayer1Score++;
                } else if(val == PLAYER2)
                    currentPlayer2Score++;
                }
            }
        
        gameFrame.getPlayerView().setPlayer1Score(currentPlayer1Score);
        gameFrame.getPlayerView().setPlayer2Score(currentPlayer2Score);
    }
    
    public static GamesModel gamesModel() {
    	return gamesModel;
    }
    
    public static void showDialog(String state, String message) {
        gameFrame.getScoreView().refreshScore();
        int result = JOptionPane.showConfirmDialog(gameFrame,message,state,JOptionPane.YES_NO_OPTION);
        if(result == 0) {
            gamesModel.reset();
            update();
            Command.subscribe(gamesModel.getSelectedGame());
            appendHistory("--------Next Round--------");
        } else {
            gameFrame.dispose();
            gamesModel = null;
        }
    }
    
    public static void showErrorDialog(String state, String message) {
        JOptionPane.showMessageDialog(gameFrame, message, state, JOptionPane.ERROR_MESSAGE);
    }
    
    public void doMove(MouseEvent e) {
        int x = (int) Math.floor(e.getX()/BoardCanvas.getCellSize());
        int y = (int) Math.floor(e.getY()/BoardCanvas.getCellSize());
        int move = (gamesModel.getBoard().getWidth()*y)+x;
        System.out.println(move);
        Command.move(move);
    }
    
    public static void placePiece(String player, int move) {
    	int col = move%gamesModel.getBoard().getWidth();
    	int row = move/gamesModel.getBoard().getHeight();
    	gamesModel.getBoard().place(col, row, gamesModel.getPlayersHashMap().get(player));
        game.updateBoard(player, move);
        update();
    }
    
    public static void update() {	
    	boardCanvas.repaint();
    }

    public void mouseMoved(MouseEvent e) {
        int x = (int) Math.floor(e.getX()/BoardCanvas.getCellSize()) * BoardCanvas.getCellSize();
        int y = (int) Math.floor(e.getY()/BoardCanvas.getCellSize()) * BoardCanvas.getCellSize();
        boardCanvas.drawSelection(x, y);
        update();
    }
}
